﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using BonSaiTableAdapters;

public partial class New_RepresentImage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            RadUpload1.AllowedFileExtensions = new string[] { ".JPG", ".PNG" };
            if (Request.QueryString["newID"] != null)
            {
                try
                {
                    int newID = Convert.ToInt32(Request.QueryString["newID"].ToString());
                    ViewState["newID"] = newID;
                    FillNew();
                }
                catch (Exception ex)
                {
                    Response.Redirect("NewManage.aspx");
                }
            }
            else
            {
                Response.Redirect("NewMange.aspx");
            }
        }
    }

    private void FillNew()
    {
        if (ViewState["newID"] != null)
        {
            int newID = Int32.Parse(ViewState["newID"].ToString());
            BonSai.NewsDataTable newDT = new NewsTableAdapter().GetNewByID(newID);
            if (newDT.Rows.Count >0)
            {
                BonSai.NewsRow newRow = newDT.Rows[0] as BonSai.NewsRow;
                if(!newRow.IsRepresentImageNull())
                {
                    imgNewRepresent.ImageUrl = "~/img_news/" + newRow.RepresentImage;
                }
                if (!newRow.IsTitleNull())
                {
                    lblNewTitle.Text = newRow.Title;
                }
                lblNewID.Text = newID.ToString();
            }
            else
            {
                 Response.Redirect("NewManage.aspx");
            }
         }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {

        if (RadUpload1.InvalidFiles.Count > 0)
        {
            lblImageType.Text = "Bạn chỉ được nhập loại  file .JPG & .PNG !";
            return;
        }

        if (ViewState["newID"] != null)
        {
            int newID = Convert.ToInt32(ViewState["newID"].ToString());


            ///THEM FILE MOI
            NewsTableAdapter newDA = new NewsTableAdapter();
            string representImage = null;

            if (RadUpload1.UploadedFiles.Count > 0)
            {
                foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
                {
                    System.IO.FileInfo f = new System.IO.FileInfo(RadUpload1.UploadedFiles[0].FileName);
                    String oldFileName = f.Name;
                    String newFileName = Server.MapPath(".") + "\\..\\img_news\\" + newID + f.Extension;
                    if (System.IO.File.Exists(newFileName))
                        System.IO.File.Delete(newFileName);

                    System.IO.File.Move(Server.MapPath(".") + "\\..\\img_news\\" + f.Name, newFileName);
                    representImage = newID + f.Extension;
                }
            }
            int i = newDA.UpdateRepresentImageByNewID(representImage, newID);
            Response.Redirect("NewDetails.aspx?newID="+ newID +"&code=image");
        }
        else
            return;
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        if (ViewState["newID"] != null)
        {
            int newID = Convert.ToInt32(ViewState["newID"].ToString());
            Response.Redirect("NewDetails.aspx?newID=" + newID);
        }
    }
}
